<?php
/*
 * Plugin Name: AWPCP Featured Ads Premium Module
 * Plugin URI: http://awpcp.com/premium-modules/featured-ads-module
 * Version: 2.0
 * Description: Allows you to offer featured ads. Includes a widget to display featured ads in your sidebar(s)
 * Author: D. Rodenbaugh
 * Author URI: http://www.skylineconsult.com
 */


add_action('plugins_loaded', 'awpcp_featured_ads_setup', 20);
function awpcp_featured_ads_setup() {
	global $awpcp, $awpcp_db_version;

	if (!$awpcp) {
		// plugin not installed?
	} else if (!$awpcp->updated()) {
		// plugin installed but db versions differs from plugin version. 
		// upgrade is required
	} else if (version_compare($awpcp_db_version, '2.0') < 0) {
		add_action('admin_notices', 'awpcp_featured_ads_notices');
	} else {
		add_action('init', 'awpcp_featured_ads_init');
	}
}


function awpcp_featured_ads_notices() {
	global $awpcp_db_version;
	$message = '<strong>Warning:</strong> The AWPCP Featured Ads module requires AWPCP version 2.0 or newer!';
    echo '<div class="error"><p>' . $message . '</p></div>';
}


function awpcp_featured_ads_init() {
	add_action('wp_head', 'awpcp_featured_ads_css',99999);
	add_action('widgets_init', create_function('', 'return register_widget("featured_ads_widget");'));
}


function awpcp_featured_ads_css() {
	$url = WP_CONTENT_URL . '/plugins/' . str_replace(basename( __FILE__),"",plugin_basename(__FILE__));
	echo '<style type="text/css" media="screen">@import "'.$url.'awpcp_featured_ads.css";</style>';
}


function awpcp_is_featured_ad_price($termid='') {
	global $wpdb;
	$tbl_adfees = $wpdb->prefix . "awpcp_adfees";

	if (intval($termid) <=0) return false;

	$query = "select is_featured_ad_pricing from $tbl_adfees where adterm_id = $termid ";
	$res = $wpdb->get_var($query);

	if ($res)
	return true;
	else
	return false;
}


function awpcp_is_featured_ad($adid) {
	global $wpdb;
	$tbl_ads = $wpdb->prefix . "awpcp_ads";

	if (intval($adid) <=0) return false;

	$query = "select is_featured_ad from $tbl_ads where ad_id = $adid ";
	$res = $wpdb->get_var($query);

	if ($res)
	return true;
	else
	return false;

}


function awpcp_featured_ad_class($adid, $layoutcode) {
	$isfeaturedclass = 'awpcp_featured_ad_wrapper';
	if (awpcp_is_featured_ad($adid))
	return str_replace("\$isfeaturedclass",$isfeaturedclass,$layoutcode);
	else
	return $layoutcode;
}


function awpcp_show_featured_ads($title, $ads_displayed, $show_thumbs, $show_no_image, $class='') {
	echo awpcp_get_featured_ads($title, $ads_displayed, $show_thumbs, $show_no_image, $class);
}


function awpcp_get_featured_ads($title, $ads_displayed, $show_thumbs, $show_no_image, $class='') {
	global $wpdb, $awpcp_imagesurl;
	$tbl_ads = $wpdb->prefix . "awpcp_ads";
	$tbl_ad_photos = $wpdb->prefix . "awpcp_adphotos";

	if ('' == $class) {
		$class = 'featured_ad_item';
	}

	$query = "SELECT ad_id, ad_title, ad_details FROM " . AWPCP_TABLE_ADS . " ";
	$query.= "WHERE is_featured_ad = 1 AND disabled <> 1 ";
	$query.= "ORDER BY RAND() limit " . $ads_displayed;
	$ads = $wpdb->get_results($query, ARRAY_A);

	$out = $title;

	if (!$ads)
	$out .= "<ul><li>".__("No featured ads yet!","AWPCP")."</li></ul>";
	else {
		$i = 0;
		$out .=  '<ul>';
		foreach ($ads as $ad) {
			$i++;
			$url = url_showad($ad['ad_id']);
			$adpic=get_a_random_image($ad['ad_id']);

			if ('' != $adpic ) {

				$img="<a class=\"thickbox\" href=\"$url\"><img class=\"widget_thumbshow\" src=\"".AWPCPTHUMBSUPLOADURL."/$adpic\"/></a>";

			} else if ('' == $adpic && $show_no_image) {

				$img = "<a class=\"thickbox\" href=\"$url\"><img class=\"widget_thumbshow\" src=\"$awpcp_imagesurl/adhasnoimage.gif\"></a>";

			} else {

				$img = '';

			}

			$excerpt = stripslashes(substr($ad['ad_details'], 0, 50)) . "...";
			$read_more = "<a href=\"".$url."\">[" . __("Read more", "AWPCP") . "]</a>";

			$out .= "<li class=\"featured_ad_item\">";
			$out .= "<div class=\"awpcplatestbox\">";
			$out .= "<div class=\"awpcplatestthumb\">".$img."</div>";
			$out .= "<h3><a href=\"".$url."\">".stripslashes($ad['ad_title']).'</a></h3>';
			$out .= "<p>".$excerpt."<br/>".$read_more."</p>";
			$out .= "</div></li>";
			if ($i >= intval($ads_displayed)) break;
		}
		$out .= '</ul>';
	}

	return $out;
}


class featured_ads_widget extends WP_Widget {

	function featured_ads_widget() {
		parent::WP_Widget(false, $name = __('AWPCP Featured Ads','AWPCP') );
	}

	function widget($args, $instance) {
		$cnt = $instance['number'];
		if (intval($cnt) <= 0) $cnt = 10;

		extract( $args );
		$title = apply_filters('widget_title', $instance['featured_title']);
		$thumbs = $instance['thumbs'];
		$show_no_image = $instance['show_no_image'];
		if ('' != $before_title) {
			$before_title = '<div id="awpcp_featured_ads_widget_title">'.$before_title;
			$after_title = '</div>';
		}

		echo $before_widget;
		if ( $title ) $title = $before_title . $title . $after_title;
		awpcp_show_featured_ads($title, $cnt, $thumbs, $show_no_image);
		echo $after_widget;
	}

	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['featured_title'] = strip_tags($new_instance['featured_title']);
		$instance['number'] = intval(strip_tags($new_instance['number']));
		$instance['thumbs'] = strip_tags($new_instance['thumbs']);
		$instance['show_no_image'] = strip_tags($new_instance['show_no_image']);
		return $instance;
	}

	function form($instance) {
		$featured_title = esc_attr($instance['featured_title']);
		$number = intval(esc_attr($instance['number']));
		$thumbs = esc_attr($instance['thumbs']);
		$show_no_image = esc_attr($instance['show_no_image']);
		?>
<p><label for="<?php echo $this->get_field_id('featured_title'); ?>"> <?php _e('Title:','AWPCP'); ?>
<input id="<?php echo $this->get_field_id('featured_title'); ?>"
	name="<?php echo $this->get_field_name('featured_title'); ?>"
	type="text" value="<?php echo $featured_title; ?>" /> <br />
<br />
		<?php _e('Number of Items to Show:','AWPCP'); ?> <input size='3'
	style="width: 40px" id="<?php echo $this->get_field_id('number'); ?>"
	name="<?php echo $this->get_field_name('number'); ?>" type="text"
	value="<?php echo $number; ?>" /> <br />
<br />
		<?php _e('Show Thumbnails in Widget?:','AWPCP'); ?> <input
	id="<?php echo $this->get_field_id('thumbs'); ?>"
	name="<?php echo $this->get_field_name('thumbs'); ?>" type="checkbox"
	value="1" <?php if ($thumbs) echo "checked='checked'" ?> /> <br />
<br />
		<?php _e('Show "No Image" PNG when ad has no picture (improves layout)?:','AWPCP'); ?>
<input id="<?php echo $this->get_field_id('show_no_image'); ?>"
	name="<?php echo $this->get_field_name('show_no_image'); ?>"
	type="checkbox" value="1"
	<?php if ($show_no_image) echo "checked='checked'" ?> /> </label></p>
	<?php
	}

}


function awpcp_featured_ads(){
	// dummy function to use for module presence tests - do not remove!
}


function awpcp_featured_ads_price_new() {
	$awpcpfeeform.="<br/><input type=\"checkbox\" name=\"rec_period\" value=\"1\" /></p>";
	$awpcpfeeform.=" ".__("Featured Ad Pricing","AWPCP")."<br/><br/>";
	return $awpcpfeeform;
}


function awpcp_featured_ads_price_config($is_featured_ad_pricing) {
	$awpcpfeeform.="<br/><input type=\"checkbox\" name=\"is_featured_ad_pricing\" value=\"1\" ";
	if ($is_featured_ad_pricing) $awpcpfeeform.= "checked=\"checked\" ";
	$awpcpfeeform.="/></p>";
	$awpcpfeeform.=" Featured Ad Pricing<br/><br/>";
	return $awpcpfeeform;
}


function awpcp_featured_ad_checking($adterm_id) {
	if (awpcp_is_featured_ad_price($adterm_id))
	$is_featured_ad = 1;
	else
	$is_featured_ad = 0;

	if (1 == $_REQUEST['makefeatured'])
	$is_featured_ad = 1;

	return $is_featured_ad;
}


function awpcp_featured_ad_checking2($adterm_id) {
	if (awpcp_is_featured_ad_price($adterm_id))
	$is_featured_ad = 1;
	else
	$is_featured_ad = 0;

	if (!$is_featured_ad) {
		if (1 == $_REQUEST['makefeatured'])
		$is_featured_ad = 1;
		else
		$is_featured_ad = 0;
	}
	if ($is_featured_ad) {
		if (1 == $_REQUEST['makenonfeatured'])
		$is_featured_ad = 0;
	}
	return $is_featured_ad;
}


function awpcp_make_featured_link($ad_id, $offset, $results) {
	if ('' == trim($offset)) $offset = 0;
	if (awpcp_is_featured_ad($ad_id)) {
		$makefeaturedlink ="<a href=\"?page=Manage1&action=makenonfeatured&id=$ad_id&offset=$offset&results=$results\">";
		$makefeaturedlink  .=__("Remove Featured","AWPCP");
		$makefeaturedlink .="</a> | ";
	} else {
		$makefeaturedlink ="<a href=\"?page=Manage1&action=makefeatured&id=$ad_id&offset=$offset&results=$results\">";
		$makefeaturedlink  .=__("Make Featured","AWPCP");
		$makefeaturedlink .="</a> | ";
	}
	return $makefeaturedlink;
}


function awpcp_make_featured_head() {
	return "<th>".__('Featured Ad',"AWPCP")."</th>";
}


function awpcp_make_featured_note($is_featured='') {
	if ($is_featured)
	return "<td>".__("Yes","AWPCP")."</td>";
	else
	return "<td>".__("No","AWPCP")."</td>";
}


function awpcp_make_featured_sort($sortby='') {
	if ($sortby == 'featured')
	{
		$output .= " &nbsp;&nbsp;&nbsp;&nbsp;<b>| ";
		$output .= __("Featured Ads First","AWPCP");
		$output .= " |</b> ";
	}
	else
	{
		$output .= " &nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?page=Manage1&sortby=featured\">";
		$output .= __("Featured Ads First","AWPCP");
		$output .= "</a>";
	}
	return $output;
}


function awpcp_featured_ad_parms() {
	return clean_field($_REQUEST['is_featured_ad_pricing']);
}


function awpcp_featured_ad_edit($adid) {
	$theformbody .= "<input type=\"checkbox\" name=\"makefeatured\" value=\"1\" ";
	if (awpcp_is_featured_ad($adid))
	$theformbody .= " checked='checked' ";
	$theformbody .= "> ";
	$theformbody .= __("Make this ad a Featured Ad","AWPCP");
	$theformbody .= "<br/>";
	return $theformbody;
}

?>